package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /*
     * 新增套餐
     * */
    @Transactional(rollbackFor = Exception.class)
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.insert(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(item -> {
            item.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insert(setmealDishes);
    }

    /*
     * 分页查询
     * */
    @Override
    public PageResult pageSelect(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageSelect(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();
        return new PageResult(total, result);
    }

    /*
     * 根据id查询套餐
     * */
    public SetmealVO getSetmealById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectById(id);
        List<SetmealDish> list = setmealDishMapper.selectBySetmealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /*
     * 批量删除套餐
     * */
    public void delteSetmeals(List<Long> ids) {
        // 查询开启状态的套餐数量
        Integer total = setmealMapper.selectOpenStatusSetmealCount(ids, StatusConstant.ENABLE);
        if (total > 0) throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        setmealMapper.deleteSetmeals(ids);
    }

    /*
     * 修改套餐
     * */
    @Transactional(rollbackFor = Exception.class)
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(item -> item.setSetmealId(setmeal.getId()));
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        setmealDishMapper.insert(setmealDishes);
        setmealMapper.update(setmeal);
    }

    /*
     * 套餐起售、停售
     * */
    @Transactional(rollbackFor = Exception.class)
    public void updateStatus(Integer status, Long id) {
        if (status.equals(StatusConstant.ENABLE)) {
            Integer total = setmealDishMapper.selectCloseStatusCount(id, StatusConstant.DISABLE);
            if (total > 0) throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
        setmealMapper.update(
                Setmeal.builder()
                        .id(id)
                        .status(status)
                        .build()
        );
    }

    /*
    * 根据分类id查询套餐
    * */
    public List<Setmeal> getSetmealByCategoryId(Integer categoryId) {
        List<Setmeal> list = setmealMapper.getSetmealByCategoryId(categoryId,StatusConstant.ENABLE);

        return list;
    }

}
